<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃      
 * Date: 2015-09-09
 */

namespace Seller\Controller;

use Seller\Logic\UsersLogic;
use Think\AjaxPage;
use Think\Page;

class UserController extends BaseController {

    public function index(){
        
        $this->display();
    }

    /**
     * 会员列表
     */
    public function ajaxindex(){
        // 搜索条件
       
        $condition = array();
        $email=I('email') ? $condition['email'] = I('email') : false;
        $mobile=I('mobile');
        $condition['mobile']=array('like',"%$mobile%");
        $tguser_input=I('tguser');
        $bz=I('bz') ? $condition['bz'] = I('bz') : false;
        $nickname=I('nickname');
        $condition['nickname']=array('like',"%$nickname%");
        $tjr_cs=I('tjr_cs') ? $condition['tjr_cs'] = I('tjr_cs') : false;
        $sort_order = I('order_by').' '.I('sort'); 
		 $condition['tjr_cs']=array('egt',$tjr_cs);
	    $seller_id=session('seller');
        $store_id=$seller_id['store_id'];
        $tguser = M('users')->query("select nickname,user_id  from __PREFIX__users where seller_id =".$store_id." and  group_id in(5,7)");
		$tguser_m = array();
		$tguser_n = array();
		foreach($tguser as $v){
			$tguser_m[$v['user_id']] = $v['nickname'];
			$tguser_n[$v['nickname']] = $v['user_id'];
		}
		
		$nn = in_array($tguser_input,$tguser_m);
		if($nn) {
			$tgid = $tguser_n[$tguser_input];
			$condition['tuixiao_id'] = $tgid;
		}
        if(empty($email)&&empty($mobile)){

            $seller_id=session('seller');
            $seller_id=$seller_id['store_id'];
            $condition['seller_id'] = $seller_id;
            $condition['group_id'] = '0';

            $model = M('users');
            $userList = $model->field('ty_users.*,ty_store.store_name as store_name')->join('ty_store on ty_users.seller_id=ty_store.store_id' )->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($userList as $key => &$value) {
                $user=explode(",", $value['bz']);
                $value['bz']=$user;
                $tjr_name=M('users')->field('nickname')->where(array('user_id'=>$value['tjr_id']))->find();
              $value['tjr_name']=$tjr_name['nickname'];
             } 
            

            //var_dump($userList['bz']);
        }

        $model = M('users');

        $count = $model->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
       $condition['bz']=array('like','%'.$bz.'%');
     
        $condition['group_id']='0';
        $userList = $model->field('ty_users.*,ty_store.store_name as store_name')->join('ty_store on ty_users.seller_id=ty_store.store_id' )->where($condition )->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
       //$userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();

       $seller_id=$seller_id['store_id'];

        $user_id_arr = get_arr_column($userList, 'user_id');
        foreach ($userList as $key => &$value) {
            $tjr_name=M('users')->field('nickname')->where(array('user_id'=>$value['tjr_id']))->find();
            $value['tjr_name']=$tjr_name['nickname'];
            if($value['tjr_cs']>='3'){
                $sql="select b.order_id from ty_users a ,ty_order b,ty_order_goods c, ty_goods d where a.user_id=b.user_id and b.order_id =c.order_id and c.goods_id =d.goods_id and d.zhuangtai='1' and a.user_id=$value[user_id] and b.order_status='1'";
                $Model = M();
                $order_id = $Model->query($sql);
                
                $value['order_id']=$order_id[0][order_id];

            }
        }
        //var_dump($userList);
        if(!empty($user_id_arr))
        {
            $first_leader = M('users')->query("select first_leader,count(1) as count  from __PREFIX__users where first_leader in(".  implode(',', $user_id_arr).")  group by first_leader");
            $first_leader = convert_arr_key($first_leader,'first_leader');
            
            $second_leader = M('users')->query("select second_leader,count(1) as count  from __PREFIX__users where second_leader in(".  implode(',', $user_id_arr).")  group by second_leader");
            $second_leader = convert_arr_key($second_leader,'second_leader');            
            
            $third_leader = M('users')->query("select third_leader,count(1) as count  from __PREFIX__users where third_leader in(".  implode(',', $user_id_arr).")  group by third_leader");
            $third_leader = convert_arr_key($third_leader,'third_leader');            
        }
	
		

        $this->assign('tguser_m',$tguser_m);
        $this->assign('first_leader',$first_leader);
        $this->assign('second_leader',$second_leader);
        $this->assign('third_leader',$third_leader);
                                
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    /**
     * 会员详细信息查看
     */
    public function detail(){
        $uid = I('get.id');
		$user=session('seller');
		$user_id=$user['user_id'];
        $store_id=$user['store_id'];
        //var_dump($store_id);
        //$tgy=M('users')->where(array('seller_id'=>$store_id,'tuixiao_id'=>0))->where('group_id in (5,6,7)')->select();
       
        $seller_info = D('seller')->field('user_id')->where("store_id=".$store_id." and  group_id in(5,7)")->order('group_id desc')->select();
        $seller_ids = array();
        foreach ($seller_info as $key => $value) {
           $seller_ids[] =  $value['user_id'];
        }
        $seller_ids = implode(',', $seller_ids);
       
        $tgy = D('users')->where("user_id in(".$seller_ids.")")->order('group_id desc')->select();
        


        $user = D('users')->where(array('user_id'=>$uid))->find();
		$tuixiao = D('users')->where(array('user_id'=>$user_id))->find();
		$bz1=D('bingzhong')->where(array('cat_id'=>1))->select();
		$bz2=D('bingzhong')->where(array('cat_id'=>2))->select();
		$bz3=D('bingzhong')->where(array('cat_id'=>31))->select();
		$bz4=D('bingzhong')->where(array('cat_id'=>32))->select();
		$bz5=D('bingzhong')->where(array('cat_id'=>33))->select();
		$bz6=D('bingzhong')->where(array('cat_id'=>34))->select();
		$bz7=D('bingzhong')->where(array('cat_id'=>4))->select();
		$bz8=D('bingzhong')->where(array('cat_id'=>5))->select();
		$bz = array($bz1,$bz2,$bz3,$bz4,$bz5,$bz6,$bz7,$bz8);
        //$cishu=M('order_goods')->join('ty_order on ty_order_goods.order_id = ty_order.order_id')->where(array('user_id'=>$uid))->select();
		//var_dump($cishu);
        $sql1="select * from ty_order a ,ty_goods b, ty_order_goods c where a.order_id= c.order_id and c.goods_id=b.goods_id and a.user_id='$uid' and b.good_cishu >'0' and a.order_status !='8'";
        $Model = M();
        $fuwu = $Model->query($sql1);
        //var_dump($fuwu);
		foreach($fuwu as $k => &$v){
			$count =M(liliao)->where(array('user_id'=>$uid,'order_id'=>$v['order_id']))->count();
			$v['sycs']=$v['good_cishu']*$v['goods_num']-$count;
			$v['count']=$count;
		}
		$this->assign('fuwu',$fuwu);
		if(empty($fuwu)){
			
			$user['list']='0';
		}else{
			
			$user['list']='1';
		}
		$tuixiao_id=$user['tuixiao_id'];
		$tui=M('users')->where(array('user_id'=>$tuixiao_id))->find();

		//var_dump($tui);
        $sql="select * from ty_liliao a ,ty_users b ,ty_store c ,ty_order_goods d where a.store_id=c.store_id and a.czy_id =b.user_id and a.user_id=$uid and d.order_id =a.order_id GROUP BY  a.id DESC limit 0,5 ";     
		//echo $sql;
		$Model = M();
        $result = $Model->query($sql);
        
        $xiaji="select nickname from ty_users where tjr_id=$uid ";
        $xiaxianname=M()->query($xiaji);
        //var_dump($xiaxianname);
        $tx=M('user_tx')->where(array('user_id'=>$uid))->select();
        if(!$user)
            exit($this->error('会员不存在'));
        if(IS_POST){
            //  会员信息编辑
			
            $password = I('post.password');
            $password2 = I('post.password2');
			$tizheng1=$_POST['tizheng1'] ? $_POST['tizheng1'] : '0';
            $tizheng2=$_POST['tizheng2'] ? $_POST['tizheng2'] : '0';
            $tizheng3=$_POST['tizheng3'] ? $_POST['tizheng3'] : '0';
            $tizheng4=$_POST['tizheng4'] ? $_POST['tizheng4'] : '0';
            $tizheng5=$_POST['tizheng5'] ? $_POST['tizheng5'] : '0';
			if($tizheng1=='0'){
				$tizheng1='';
			}else{
				$tizheng1=implode(",",$tizheng1);
				$_POST['tizheng1']=$tizheng1;
			}
			if($tizheng2=='0'){
				$tizheng2='';
			}else{
				$tizheng2=implode(",",$tizheng2);
				$_POST['tizheng2']=$tizheng2;
			}
			if($tizheng3=='0'){
				$tizheng3='';
			}else{
				$tizheng3=implode(",",$tizheng3);
				$_POST['tizheng3']=$tizheng3;
			}
			if($tizheng4=='0'){
				$tizheng4='';
			}else{
				$tizheng4=implode(",",$tizheng4);
				$_POST['tizheng4']=$tizheng4;
			}
			if($tizheng5=='0'){
				$tizheng5='';
			}else{
				$tizheng5=implode(",",$tizheng5);
				$_POST['tizheng5']=$tizheng5;
			}
            
            if($password != '' && $password != $password2){
                exit($this->error('两次输入密码不同'));
            }
            if($password == '' && $password2 == ''){
                unset($_POST['password']);
            }else{
                $_POST['password'] = encrypt($_POST['password']);
            }
            $store_logo=$_POST['store_logo'];
            
            foreach($store_logo as $v){
                if($v){
                     $a=M('user_tx')->add(array('user_id'=>$uid,'store_logo'=>$v));
                }
               
            }

            $row = M('users')->where(array('user_id'=>$uid))->save($_POST);
            $tuixiao_id=$_POST['tuixiao_id'];
            M('order')->where(array('user_id'=>$uid))->save(array('tuiguangid'=>$tuixiao_id));
            if($row or $a)
                exit($this->success('修改成功'));
            exit($this->error('未作内容修改或修改失败'));
        }
        
        $user['first_lower'] = M('users')->where("first_leader = {$user['user_id']}")->count();
        $user['second_lower'] = M('users')->where("second_leader = {$user['user_id']}")->count();
        $user['third_lower'] = M('users')->where("third_leader = {$user['user_id']}")->count();
		
		
		
		$this->assign('bz',$bz);
		$this->assign('tuixiao',$tuixiao);
       
		$this->assign('tui',$tui);

        $this->assign('user',$user);
        $this->assign('xiaxianname',$xiaxianname);
	    $this->assign('tgy',$tgy);
        $this->assign('result',$result);
         $this->assign('tx',$tx);
        $this->display();
    }
    public function add_user(){
        $store=session('seller');
        $store_id=$store['store_id'];

        $seller_info = D('seller')->field('user_id')->where("store_id=".$store_id." and  group_id in(5,7)")->order('group_id desc')->select();
	    $seller_ids = array();
		
        foreach ($seller_info as $key => $value) {
           $seller_ids[] =  $value['user_id'];
        }
        $seller_ids = implode(',', $seller_ids);

        $list = D('users')->where("user_id in(".$seller_ids.")")->order('group_id desc')->select();
        
		$bz1=D('bingzhong')->where(array('cat_id'=>1))->select();
		$bz2=D('bingzhong')->where(array('cat_id'=>2))->select();
		$bz3=D('bingzhong')->where(array('cat_id'=>31))->select();
		$bz4=D('bingzhong')->where(array('cat_id'=>32))->select();
		$bz5=D('bingzhong')->where(array('cat_id'=>33))->select();
		$bz6=D('bingzhong')->where(array('cat_id'=>34))->select();
		$bz7=D('bingzhong')->where(array('cat_id'=>4))->select();
		$bz8=D('bingzhong')->where(array('cat_id'=>5))->select();
		$bz = array($bz1,$bz2,$bz3,$bz4,$bz5,$bz6,$bz7,$bz8);
        if(IS_POST){
            $data = I('post.');
            $reg_time=$_POST['reg_time'];
            
            if($reg_time){
                $data['reg_time']=strtotime($reg_time);
            }else{
                $data['reg_time']=time();
            }
            $tizheng1=$_POST['tizheng1'] ? $_POST['tizheng1'] : '0';
            $tizheng2=$_POST['tizheng2'] ? $_POST['tizheng2'] : '0';
            $tizheng3=$_POST['tizheng3'] ? $_POST['tizheng3'] : '0';
            $tizheng4=$_POST['tizheng4'] ? $_POST['tizheng4'] : '0';
            $tizheng5=$_POST['tizheng5'] ? $_POST['tizheng5'] : '0';
			if($tizheng1=='0'){
				$tizheng1='';
			}else{
				$tizheng1=implode(",",$tizheng1);
				$data['tizheng1']=$tizheng1;
			}
			if($tizheng2=='0'){
				$tizheng2='';
			}else{
				$tizheng2=implode(",",$tizheng2);
				$data['tizheng2']=$tizheng2;
			}
			if($tizheng3=='0'){
				$tizheng3='';
			}else{
				$tizheng3=implode(",",$tizheng3);
				$data['tizheng3']=$tizheng3;
			}
			if($tizheng4=='0'){
				$tizheng4='';
			}else{
				$tizheng4=implode(",",$tizheng4);
				$data['tizheng4']=$tizheng4;
			}
			if($tizheng5=='0'){
				$tizheng5='';
			}else{
				$tizheng5=implode(",",$tizheng5);
				$data['tizheng5']=$tizheng5;
			}
            $data['roleid']=$store['roleid'];
            $user_obj = new UsersLogic();
            $res = $user_obj->addUser($data);
            if($res['status'] == 1){
                $this->success('添加成功',U('User/index'));exit;
            }else{
                $this->error('添加失败,'.$res['msg'],U('User/index'));
            }
        }
        $seller_id=session('seller');
        $this->assign('list',$list);
		$this->assign('bz',$bz);
        $this->assign('seller_id',$seller_id);
        $this->display();
    }
    /**
     * 用户收货地址查看
     */
    public function address(){
        $uid = I('get.id');
        $lists = D('user_address')->where(array('user_id'=>$uid))->select();
        $regionList = M('Region')->getField('id,name');
        $this->assign('regionList',$regionList);
        $this->assign('lists',$lists);
        $this->display();
    }

    /**
     * 删除会员
     */
    public function delete(){
        $uid = I('get.id');
        $row = M('users')->where(array('user_id'=>$uid))->delete();
        if($row){
            $this->success('成功删除会员');
        }else{
            $this->error('操作失败');
        }
    }
    public function del(){
        $id = I('id');
        $row = M('user_tx')->where(array('id'=>$id))->delete();
        if($row){
            $this->success('成功删除');
        }else{
            $this->error('操作失败');
        }
    }
    public function order(){
        $id = I('get.id');
         $row = M('order')->where("order_id=$id")->save(array('order_status'=>8));
        if($row){
            $this->success('退款成功');
        }else{
            $this->error('操作失败');
        }
    }
    /**
     * 账户资金记录
     */
    public function account_log(){
        $user_id = I('get.id');
        //获取类型
        $type = I('get.type');
        //获取记录总数
        $count = M('account_log')->where(array('user_id'=>$user_id))->count();
        $page = new Page($count);
        $lists  = M('account_log')->where(array('user_id'=>$user_id))->order('change_time desc')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('user_id',$user_id);
        $this->assign('page',$page->show());
        $this->assign('lists',$lists);
        $this->display();
    }
    
    public function account_edit(){
        $user_id = I('get.id');
        if(!$user_id > 0)
            $this->error("参数有误");
        if(IS_POST){

            //获取操作类型
            $m_op_type = I('post.money_act_type');
            $user_money = I('post.user_money');
            $user_money =  $m_op_type ? $user_money : 0-$user_money;

            $p_op_type = I('post.point_act_type');
            $pay_points = I('post.pay_points');
            $pay_points =  $p_op_type ? $pay_points : 0-$pay_points;

            $f_op_type = I('post.frozen_act_type');
            $frozen_money = I('post.frozen_money');
            $frozen_money =  $f_op_type ? $frozen_money : 0-$frozen_money;

            $desc = I('post.desc');
            if(!$desc)
                $this->error("请填写操作说明");
            if(accountLog($user_id,$user_money,$pay_points,$desc)){
                $this->success("操作成功",U("Seller/User/account_log",array('id'=>$user_id)));
            }else{
                $this->error("操作失败");
            }
            exit;
        }
        $this->assign('user_id',$user_id);
        $this->display();
    }
    /**
     * 账户资金调节
     */
    public function return_goods(){        
        $desc = I('post.desc');
        $return_goods_id = I('return_goods_id');
        $return_goods = M('return_goods')->where("id = $return_goods_id and store_id = ".STORE_ID)->find();
        empty($return_goods) && $this->error("参数有误");
        
        $user_id = $return_goods['user_id'];
        $order_goods = M('order_goods')->where("order_id ={$return_goods['order_id']} and goods_id = {$return_goods['goods_id']} and spec_key = '{$return_goods['spec_key']}'")->find();
        if($order_goods['is_send'] != 1)        
            $this->error("该订单商品状态不能退款操作");        
/*
        $order = M('order')->where("order_id = {$return_goods['order_id']}")->find();
        
        
        // 计算退回积分公式
        //  退款商品占总商品价比例 =  (退款商品价 * 退款商品数量)  / 订单商品总价      // 这里是算出 退款的商品价格占总订单的商品价格的比例 是多少
        //  退款积分 = 退款比例  * 订单使用积分
         
        // 退款价格的比例        
        $return_price_ratio = ($order_goods['member_goods_price'] * $order_goods['goods_num']) / $order['goods_price'];
        // 退还积分 = 退款价格的比例 * 
        $return_integral = ceil($return_price_ratio * $order['integral']);        
        
         // 退还金额 = (订单商品总价 - 优惠券 - 优惠活动) * 退款价格的比例 - (退还积分 + 当前商品送出去的积分) / 积分换算比例    
         // 因为积分已经退过了, 所以退金额时应该把积分对应金额推掉 其次购买当前商品时送出的积分也要退回来,以免被刷积分情况
                       
        $return_goods_price = ($order['goods_price'] - $order['coupon_price'] - $order['order_prom_amount']) * $return_price_ratio - ($return_integral + $order_goods['give_integral']) /  tpCache('shopping.point_rate');
        $return_goods_price = round($return_goods_price,2); // 保留两位小数
 */       
        
        $refund = order_settlement($return_goods['order_id'],$order_goods['rec_id']);  // 查看退款金额        
      //  print_r($refund);
        $return_goods_price = $refund['refund_settlement'] ? $refund['refund_settlement'] : 0; // 这个商品的退款金额
        //$refund_integral = $refund['refund_integral'] ? ($refund['refund_integral'] * -1) : 0; // 这个商品的退积分
        $refund_integral = $refund['refund_integral'] - $refund['give_integral'];
        
        
        if(IS_POST)
        {            
            if(!$desc)
                $this->error("请填写操作说明");
            if(!$user_id > 0)
                $this->error("参数有误");
                        
            $pending_money = M('store')->where(" store_id = ".STORE_ID)->getField('pending_money'); // 商家在未结算资金 
            if($pending_money < $return_goods_price)
                $this->error("你的未结算资金不足 ￥{$return_goods_price}");
             
           //     M('store')->where(" store_id = ".STORE_ID)->setDec('pending_money',$user_money); // 从商家的 未结算自己里面扣除金额                           
            $result = storeAccountLog(STORE_ID, 0,$return_goods_price * -1,$desc,$return_goods['order_id']);
            if($result)
            {                
                accountLog($user_id,$return_goods_price,$refund_integral,'订单退货',0,$return_goods['order_id']);
            }  else {
                 $this->error("操作失败");
            }             
            M('order_goods')->where("rec_id = {$order_goods['rec_id']}")->save(array('is_send'=>3));//更改商品状态 
            // 如果一笔订单中 有退货情况, 整个分销也取消                      
            M('rebate_log')->where("order_id = {$return_goods['order_id']}")->save(array('status'=>4,'remark'=>'订单有退货取消分成'));
                                
            $this->success("操作成功",U("Order/return_list"));
            exit;
        }
                
        $this->assign('return_goods_price',$return_goods_price);
        $this->assign('return_integral',$refund_integral);
        $this->assign('order_goods',$order_goods);
        $this->assign('user_id',$user_id);
        $this->display();
    }
    
    public function level(){
    	$act = I('GET.act','add');
    	$this->assign('act',$act);
    	$level_id = I('GET.level_id');
    	$level_info = array();
    	if($level_id){
    		$level_info = D('user_level')->where('level_id='.$level_id)->find();
    		$this->assign('info',$level_info);
    	}
    	$this->display();
    }
    
    public function levelList(){
    	$Ad =  M('user_level');
    	$res = $Ad->where('1=1')->order('level_id')->page($_GET['p'].',10')->select();
    	if($res){
    		foreach ($res as $val){
    			$list[] = $val;
    		}
    	}
    	$this->assign('list',$list);
    	$count = $Ad->where('1=1')->count();
    	$Page = new \Think\Page($count,10);
    	$show = $Page->show();
    	$this->assign('page',$show);
    	$this->display();
    }
    
    public function levelHandle(){
    	$data = I('post.');
    	if($data['act'] == 'add'){
    		$r = D('user_level')->add($data);
    	}
    	if($data['act'] == 'edit'){
    		$r = D('user_level')->where('level_id='.$data['level_id'])->save($data);
    	}
    	 
    	if($data['act'] == 'del'){
    		$r = D('user_level')->where('level_id='.$data['level_id'])->delete();
    		if($r) exit(json_encode(1));
    	}
    	 
    	if($r){
    		$this->success("操作成功",U('Admin/User/levelList'));
    	}else{
    		$this->error("操作失败",U('Admin/User/levelList'));
    	}
    }

    /**
     * 搜索用户名
     */
    public function search_user()
    {
        $search_key = trim(I('search_key'));        
        if(strstr($search_key,'@'))    
        {
            $list = M('users')->where(" email like '%$search_key%' ")->select();        
            foreach($list as $key => $val)
            {
                echo "<option value='{$val['user_id']}'>{$val['email']}</option>";
            }                        
        }
        else
        {   
            
            $sql="select * from ty_users where is_distribut=1 and (mobile like '%$search_key%' or nickname like '%$search_key%')";
          
            $list=M()->query($sql);
            if(empty($list)){
                $this->ajaxReturn(json_encode(1));
            }else{
                $listu = array();
                foreach($list as $key => $val)
                {
                    $val['option'] =  "<option value='{$val['user_id']}'>{$val['nickname']}</option>";
                    $listu[] = $val;
                }  
                $this->ajaxReturn(json_encode($listu));
            }
            
        } 
        exit;
    }
    
    /**
     * 分销树状关系
     */
    public function ajax_distribut_tree()
    {
          $list = M('users')->where("first_leader = 1")->select();
          $this->display();
    }
    public function liliao(){
        $data['user_id']=$_POST['user_id'];
        $data['czy_id']=$_POST['group_id'];
		//var_dump($_POST['group_id']);
        $data['order_id']=$_POST['order_id'];
        $store=session('seller');
        $data['store_id']=$store['store_id'];
        $data['shijian']=time();
        $m=M("liliao");
        $result =$m->add($data);
       if($result){
           $this->success('操作成功',true);
       }
        
    }
	public function user_print(){
    	$id = I('id');
		
		$sql="select * from ty_liliao a ,ty_users b ,ty_store c ,ty_order_goods d,ty_goods e where a.store_id=c.store_id and a.czy_id =b.user_id  and d.order_id =a.order_id and d.goods_id = e.goods_id and a.id ='{$id}' GROUP BY  a.id";
        $Model = M();
        $list = $Model->query($sql);
	    $name=M('liliao')->join('ty_users  on ty_liliao.user_id=ty_users.user_id')->where(array('id'=>$id))->find();
        //var_dump($name);
        foreach($list as $k => &$v){
            $count =M(liliao)->where(array('order_id'=>$v['order_id']))->count();
            $v['sycs']=$v['good_cishu']-$count;
            $v['count']=$count;
            $v['name']=$name['nickname'];

        }

		$this->assign('list',$list);
        $this->display();
    }
    public function sear_user()
    {
        $search_key = trim(I('search_key'));        
        if(strstr($search_key,'@'))    
        {
            $list = M('users')->where(" email like '%$search_key%' ")->select();        
            foreach($list as $key => $val)
            {
                echo "<option value='{$val['user_id']}'>{$val['email']}</option>";
            }                        
        }
        else
        {
            $list = M('users')->where(" mobile like '%$search_key%'  ")->select(); 
            $listu = array();
            foreach($list as $key => $val)
            {
                $val['option'] =  "<option value='{$val['user_id']}'>{$val['mobile']}</option>";
                $listu[] = $val;
            }  
            $this->ajaxReturn(json_encode($listu));

        } 
        exit;
    }

    public function data_re()
    {
        $list =  M('users')->where("reg_time = '' ")->select();

        foreach ($list as $key => $value) {
           $n =  M('order')->where("user_id = '".$value['user_id']."' ")->find();
           $r = D('users')->where('user_id='.$value['user_id'])->save(array('reg_time'=>$n['add_time']));  
            
        }     
     
           
        
    }
    
    
}